package com.pda.yuanjing.pages.kcpd.add;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanin.AddCBillBeanIn;
import com.pda.yuanjing.beanout.JDBeanNewOut;
import com.pda.yuanjing.beanout.PDPBeanOut;
import com.pda.yuanjing.database.InStore;
import com.pda.yuanjing.dialog.product.ProductDialog;
import com.pda.yuanjing.dialog.store.StoreDialog;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.DateUtils;
import com.public_module.tools.SharedPreferencesManger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PDBillAddActivity extends BaseActivity<AddBillModel, AddBillView, AddBillPresent> implements AddBillView {
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.tvbill)
    EditText tvbill;
    @BindView(R.id.tvstore)
    TextView tvstore;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.tvproduct)
    TextView tvproduct;
    @BindView(R.id.ll2)
    LinearLayout ll2;
    @BindView(R.id.edcode)
    EditText edcode;
    @BindView(R.id.logine)
    Button logine;
    @BindView(R.id.ll3)
    LinearLayout ll3;
    @BindView(R.id.listview)
    SwipeMenuListView listview;
    @BindView(R.id.tvb)
    TextView tvb;
    @BindView(R.id.tvbatch)
    EditText tvbatch;

    private String TAG = "BillAddActivity";
    private OptionsPickerView pickerView;
    JDBeanNewOut.DataBean dataBean = null;
    ProductDialog productDialog;
    StoreDialog storeDialog;
    String sstr = "";
    String pstr = "";
    int cnum = 0;
    ProductAdapter productAdapter = null;
    List<InStore> list = new ArrayList<>();
    List<PDPBeanOut.DataBean> listt = new ArrayList<>();

    protected void initdata() {
        dataBean = (JDBeanNewOut.DataBean) getIntent().getSerializableExtra(PDApp.process);
        tvTitle.setText(dataBean.getJieDianName() + " " + SharedPreferencesManger.getRealName());
        tvbill.setText(dataBean.getProcessID() + DateUtils.stampToDateTime(new Date().getTime()));
        View headView = getLayoutInflater().inflate(R.layout.head_isscanf, null);
        listview.addHeaderView(headView, null, false);
        listview.setHeaderDividersEnabled(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addpd);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.iv_title_left, R.id.logine, R.id.tv_right, R.id.tvproduct, R.id.tvstore})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.logine:
                if (tvbill.getText().toString().length() == 0) {
                    showMsg("请填写单号");
                    return;
                }
                if (edcode.getText().toString().length() == 0) {
                    showMsg("请填写数量");
                    return;
                }
                if (Integer.parseInt(edcode.getText().toString()) <= 0) {
                    showMsg("应扫数必须大于0");
                    return;
                }
                if (pstr.length() == 0) {
                    showMsg("请填写产品");
                    return;
                }
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getProductID().equals(pstr)) {
                        showMsg("请填选择不同产品");
                        return;
                    }
                }
                InStore inStore = new InStore();
                inStore.setBillID(tvbill.getText().toString());
                inStore.setBillCode(tvbill.getText().toString());
                inStore.setCreateTime(DateUtils.stampToDate(new Date().getTime()));
                inStore.setDesStoreID(SharedPreferencesManger.getStoreID());
                inStore.setDesStoreName(SharedPreferencesManger.getStoreName());
                inStore.setProductID(pstr);
                inStore.setProductName(tvproduct.getText().toString());
                inStore.setCaseBoxNum(cnum);
                inStore.setYnum(Integer.parseInt(edcode.getText().toString()));
                inStore.setSnum(0);
                inStore.setBillListID(pstr + "-" + DateUtils.stampToDateTime(new Date().getTime()));
                list.add(inStore);
                productAdapter.notifyDataSetChanged();
                pstr = "";
                tvproduct.setText("");
                edcode.setText("");
                break;
            case R.id.tv_right:
                if (tvbill.getText().toString().length() == 0) {
                    showMsg("请填写单号");
                    return;
                }
//                if (tvbatch.getText().toString().length() == 0) {
//                    showMsg("请填写批次");
//                    return;
//                }
                if (sstr.equals("")) {
                    showMsg("请选择库房");
                    return;
                }
                if (listt == null || listt.size() == 0) {
                    showMsg("当前库房无产品");
                    return;
                }
                AddCBillBeanIn addCBillBeanIn = new AddCBillBeanIn();
                addCBillBeanIn.setCid(SharedPreferencesManger.getCid());
                addCBillBeanIn.setAction("SY_AddBatch");
                addCBillBeanIn.setUserID(SharedPreferencesManger.getUid());
                addCBillBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
                addCBillBeanIn.setBillCode(tvbill.getText().toString());
                addCBillBeanIn.setJieDianName("");
                addCBillBeanIn.setProcessID("-1");
                addCBillBeanIn.setBillType("300");
                addCBillBeanIn.setGongYingShangID("-1");
                addCBillBeanIn.setStoreIDIn(sstr);
                addCBillBeanIn.setBatch(tvbatch.getText().toString());
                for (int i = 0; i < listt.size(); i++) {
                    AddCBillBeanIn.DataBean dataBean = new AddCBillBeanIn.DataBean();
                    dataBean.setPlanNums(listt.get(i).getNums());
                    dataBean.setProductID(listt.get(i).getProductID());
                    dataBean.setProductName(listt.get(i).getProductInfo());
                    addCBillBeanIn.getListProduct().add(dataBean);
                }
                presenter.addList(addCBillBeanIn);
                break;
            case R.id.tvstore:
                storeDialog = new StoreDialog(mContext, R.style.CommentDialog, new StoreDialog.PriorityListener() {
                    @Override
                    public void setActivityText(String str, String str1) {
                        tvstore.setText(str);
                        sstr = str1;
                        AddCBillBeanIn addCBillBeanIn = new AddCBillBeanIn();
                        addCBillBeanIn.setCid(SharedPreferencesManger.getCid());
                        addCBillBeanIn.setAction("SY_DownloadKuCunForPD");
                        addCBillBeanIn.setUserID(SharedPreferencesManger.getUid());
                        addCBillBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
                        addCBillBeanIn.setStoreIDIn(sstr);
                        presenter.getPlist(addCBillBeanIn);
                        storeDialog.dismiss();
                    }
                });
                WindowManager.LayoutParams lp = storeDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                storeDialog.show();
                break;
            case R.id.tvproduct:
                productDialog = new ProductDialog(mContext, R.style.CommentDialog, new ProductDialog.PriorityListener() {
                    @Override
                    public void setActivityText(String str, String str1, int num) {
                        tvproduct.setText(str);
                        pstr = str1;
                        cnum = num;
                        productDialog.dismiss();
                    }
                });
                WindowManager.LayoutParams lp1 = productDialog.getWindow().getAttributes();
                lp1.gravity = Gravity.CENTER;
                productDialog.show();
                break;
        }
    }

    @Override
    public void tip(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (str.length() == 0) {
                    showMsg("订单添加完成");
                    finish();
                } else {
                    showMsg(str);
                }
            }
        });
    }

    @Override
    public void tipS(List<PDPBeanOut.DataBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listt = list;
                if (listt != null) {
                    productAdapter = new ProductAdapter(PDBillAddActivity.this, listt);
                    listview.setAdapter(productAdapter);
                }
            }
        });
    }

    @Override
    public AddBillModel createModel() {
        return new AddBillModelImpl(this);
    }

    @Override
    public AddBillView createView() {
        return this;
    }

    @Override
    public AddBillPresent createPresenter() {
        return new AddBillPresent();
    }
}
